import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import {
  GetBusinessDate,
  AssetPoolGetAssetPoolName,
  GetSmallLoanCreditbalance,
  SpvCreate,
  SpvGetAllByPagedAndSort,
  GetSettlementTime,
} from "@/api/stu/SupplyChainFinance"
import { useRoute } from "vue-router";
import { ref, reactive, nextTick, watch, onMounted } from "vue";
import qs from "qs";
export function useSecuritiesSetSpv() {
  const route = useRoute()
  const dataList = ref<any>([]);
  const loading = ref<boolean>(true);
  const DialogVisible = ref<boolean>(false);
  const showDetail = ref(false)
  const projectList = ref([]) //可选的L
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      fixed: "left",
      minWidth: 50
    },
    {
      label: "spv公司名称",
      prop: "spvCompanyName",
    },
    {
      label: "资产证券化项目名称",
      prop: "assetSecuritizationName",
    },
    {
      label: "操作",
      prop: "operation",
      width: 180,
      slot: "operation"
    },
  ];
  const EntryPlatform = reactive({
    assetSecuritizationName: '',
    projectName: '特殊目的机构（SPV）',
    assetPoolId: '',
    money: '',
    BusinessDate: '',
    NewDate: '',
  })
  // 万亿 长度 14

  async function SubtimEntryPlatform() {
    if (EntryPlatform.assetPoolId == '') {
      ElMessage.warning('请选择资产证券化项目')
      return
    }
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      assetPoolId: EntryPlatform.assetPoolId,
      completedPoolBuilding: true,
      spvCompanyName: EntryPlatform.projectName,
      registeredCapital: EntryPlatform.money,
      assetSecuritizationName: EntryPlatform.assetSecuritizationName,
      businessTerm: EntryPlatform.BusinessDate + ' 00:00:00',
      lastDate: EntryPlatform.NewDate + ' 00:00:00',
      address: "****",
      postalCode: "****",
      legalRepresentativeName: "****",
      position: "****",
      businessScope: "****",
      filingMatters: "****",
      legalRepresentative: "****",
      designatedRepresentative: "****",
      paidInCapital: EntryPlatform.money,
    };
    const res = await SpvCreate(data);
    if (res.success) {
      ElMessage.success(res.msg)
      CloseEntryPlatform();
      getList()
    } else {
      ElMessage.error(res.msg)
    }
  }

  function CloseEntryPlatform() {
    EntryPlatform.assetPoolId = ''
    EntryPlatform.money = ''
    EntryPlatform.BusinessDate = ''
    DialogVisible.value = false
    showDetail.value = false
  }

  async function getList() {
    const res = await SpvGetAllByPagedAndSort({
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    });
    if (res.success) {
      dataList.value = res.data.items;
      loading.value = false;
      pagination.total = res.data.totalCount;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  async function changePro(item) {
    //获取发起日期
    let resStartData = await GetSettlementTime(qs.stringify({
      assetPoolId: item.assetPoolId
    }))
    if (resStartData.success) {
      EntryPlatform.NewDate = resStartData.data.substring(0, 10)
    } else {
      ElMessage.warning(resStartData.msg)
    }
    EntryPlatform.assetSecuritizationName = item.assetSecuritizationName
    const data = qs.stringify({
      assetPoolId: item.assetPoolId,
    })
    let res = await GetSmallLoanCreditbalance(data)
    if (res.success) {
      EntryPlatform.money = res.data
    } else {
      ElMessage.error(res.msg)
    }
    const data1 = qs.stringify({
      assetPoolId: item.assetPoolId,
    })
    let res1 = await GetBusinessDate(data)
    if (res1.success) {
      EntryPlatform.BusinessDate = res1.data.substring(0, 10)
    } else {
      ElMessage.error(res1.msg)
    }

  }
  onMounted(async () => {
    getList()
    const data = qs.stringify({
      taskId: route.query.taskId,
      planId: route.query.planId,
    })
    const res = await AssetPoolGetAssetPoolName(data);
    if (res.success) {
      projectList.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })

  function handleSizeChange() {
    getList()
  }
  function handleCurrentChange() {
    getList()
  }

  function check(row) {
    showDetail.value = true;
    DialogVisible.value = true;
    EntryPlatform.projectName = row.spvCompanyName
    EntryPlatform.assetPoolId = projectList.value.find(item => item.assetSecuritizationName == row.assetSecuritizationName).assetPoolId
    EntryPlatform.money = row.paidInCapital
    EntryPlatform.BusinessDate = row.businessTerm.substring(0, 10)
    EntryPlatform.NewDate = row.lastDate.substring(0, 10)
  }

  return {
    columns,
    loading,
    dataList,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    DialogVisible,
    EntryPlatform,
    projectList,
    CloseEntryPlatform,
    SubtimEntryPlatform,
    changePro,
    check,
    showDetail
  }
}
